var winHeight = "";
var searchCon = "";
// pages/searchGoods/searchResult/searchResult.js
var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');


Page({

  /**
   * 页面的初始数据
   */
  data: {
      searchcontent:0,
      len:1,
      goodsList: '',         //商品集合
      orderType: '',         //排序 字段
      startFlag: 2,          //分页标识
      orderType: '',         //排序 字段
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var value = options.value;
    var orderType = that.data.orderType;
    var goodsList = that.data.goodsList;
    
    that.setData({
      value: value
    });
    that.getGoodsList(value,orderType,1,10);

  },

  /**
   * 清空输入框
   */
  clearValue :function(){
    var that = this;
    // var value = that.data.value;
    // var orderType = that.data.orderType;

    that.setData({
      value: ''
    });
    
    // that.getGoodsList(value, orderType, 1, 10);
  },

  /***
   * 搜索商品
  */
  searchInput: function (e) {
    var that = this;
    var value = e.detail.value;
    var orderType = that.data.orderType;
    that.setData({
      value: value
    });

    that.getGoodsList(value, orderType, 1, 10);
  },

  /**
   * 获取 高度
   */
  getHeight: function () {
    var that = this;
      
    var query = wx.createSelectorQuery();
    var searchHeight = "";
    var goodstypeHeight = "";
    //获取头部高度
    query.select('#search').boundingClientRect()
    query.select('#goodstype').boundingClientRect()
    query.exec(function (res) {
      searchHeight = res[0].height;
      goodstypeHeight = res[1].height;

      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            searchConHeight: res.windowHeight - searchHeight - goodstypeHeight
          });
        }
      });
    });
  },

  /**
   * 初始化商品搜索
   */
  getGoodsList: function (keyword, orderType, start, length){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/goods/list", //仅为示例，并非真实的接口地址
      data: {
        keyword: keyword,
        orderType: orderType,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var goodsData = res.data.data.list;

          if (start > 1) {
            var goodsLists = that.data.goodsList;

            // 追加数据
            for (var i = 0; i < goodsData.length; i++) {
              goodsLists.push(goodsData[i])
            }
            start = parseInt(start) + 1;
          } else {
            var goodsLists = goodsData;
            start = 2;
          }
        
          that.setData({
            goodsList: goodsLists,
            startFlag: start
          })
          if (goodsLists.length != 0){
            that.getHeight(); //计算高度
          }
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 排序 点击事件
   */
  orderOnclick: function (e) {
    var that = this;
    var value = that.data.value;
    var orderType = e.currentTarget.dataset.ordertype;

    that.setData({
      orderType: orderType
    })
    that.getGoodsList(value, orderType, '1', '10');
  },

  /**
   * 京东商品下拉
   */
  searchScrollLower: function () {
    var that = this;
    var value = that.data.value;
    var start = that.data.startFlag;
    var orderType = that.data.orderType;

    that.getGoodsList(value, orderType, start, '10');
  },

  /**
  * 跳珠京东商品详情
  */
  goJdGoodsDetail: function (e) {

    var that = this;
    var goodsId = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: "../../goods/jdGoodsDetail/jdGoodsDetail?id=" + goodsId,
    })
  },

  /**
   * 跳转商品详情
   */
  goYhGoodsDetail: function (e) {
    var that = this;
    var spuid = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: "../../goods/yhjGoodsDetail/yhjGoodsDetail?spuid=" + spuid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})